package com.itjob.scott.student.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.google.gson.Gson;
import com.itjob.scott.student.entity.StudentEntity;
import com.itjob.scott.student.service.StudentService;

public class RegisterServlet extends HttpServlet {

	private static final long serialVersionUID = 7238344302762298235L;
	
	private static SimpleDateFormat format = new SimpleDateFormat("yyyy/M/d");
	private WebApplicationContext ctx = null;
	private StudentService service = null;

	@Override
	public void init(ServletConfig config) throws ServletException {
		ServletContext application = config.getServletContext();
		ctx = WebApplicationContextUtils.getWebApplicationContext(application);
		service = ctx.getBean(StudentService.class);
	}
	
	//注册
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String username = request.getParameter("username");
		
		response.setCharacterEncoding("UTF-8");
		Gson gson = new Gson();
		PrintWriter out = response.getWriter();
		Map<String, Object> returnMap = new HashMap<String, Object>();
		if(service.checkDouble(username)) {
			
			//如果用户名重复
			returnMap.put("status", "0");  //1代表成功,0代表失败
			returnMap.put("errorInfo", "用户名重复!");  //1代表成功,0代表失败
			
			String returnValue = gson.toJson(returnMap);
			out.write(returnValue);
			out.flush();
			out.close();
			return;
		} 
		
		//如果用户名不重复 往数据库中插入记录
		String password = request.getParameter("password");
		Boolean gender = request.getParameter("gender").equals("male");
		Date birthday = null;
		
		
		try {
			birthday = format.parse(request.getParameter("birthday"));
		} catch (ParseException e) {
			e.printStackTrace();
		}

		String education = request.getParameter("education");
		String[] languages = request.getParameter("langs").split(",");
		String intro = request.getParameter("intro");

		StudentEntity entity = new StudentEntity();
		entity.setUsername(username);
		entity.setPassword(password);
		entity.setGender(gender);
		entity.setBirthday(birthday);
		entity.setEducation(education);
		entity.setLanguages(languages);
		entity.setIntro(intro);
		
		//保存用户
		service.save(entity);
		returnMap.put("status", "1");  //1代表成功,0代表失败
		returnMap.put("url", "success.jsp");  //1代表成功,0代表失败
		String returnValue = gson.toJson(returnMap);
		out.write(returnValue);
		out.flush();
		out.close();
		
	}

}
